﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;


namespace ZombieMonkeys.Vibria
{
    public partial class PuzzleGrid : UserControl
    {
        public List<Logic.Square> puzzleGridList = new List<Logic.Square>();
        public List<SquareControl> puzzleGridControlList = new List<SquareControl>();

        //Create a reference to the app page of the application for global variables
        GUI.App app;

        /// <summary>
        /// constructor
        /// </summary>
        public PuzzleGrid()
        {
            InitializeComponent();


        }

        /// <summary>
        /// Event handler for clicks on squares within the control
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChildBoxClicked(object sender, EventArgs e)
        {
            foreach (SquareControl s in LayoutRoot.Children)
            {
                s.Selected = false;
                s.HighLight = false;
                //HighLightSquare();
            }
        }

        /// <summary>
        /// highlights the squares that are in the same row, column or minigrid as the selected square
        /// </summary>
        public void HighLightSquare()
        {
            int r = 0;
            int c = 0;
            int g = 0;
            for (int i = 0; i < puzzleGridControlList.Count; i++)
            {
                SquareControl s = puzzleGridControlList[i];
                if (s.Selected)
                {
                    r = s.Row;
                    c = s.Col;
                    g = s.MiniGrid;
                }
            }
            for (int i = 0; i < puzzleGridControlList.Count; i++)
            {
                SquareControl s = puzzleGridControlList[i];
                if (!s.Selected && (s.Row == r || s.Col == c || s.MiniGrid == g))
                {
                    s.HighLight = true;
                }
            }
        }

        /// <summary>
        /// Method to update the value of the square
        /// </summary>
        /// <param name="sIn">string input</param>
        public void updateSquare(String sIn)
        {
            foreach (SquareControl s in LayoutRoot.Children)
            {
                if (s.Selected)
                {
                    if (s.TextValue != "" && app.PenMode)
                    {
                        
                    }
                    else
                    {
                        s.TextValue = sIn;
                    }
                    
                }
            }
        }

        public void Update()
        {
            for (int i = 0; i < puzzleGridControlList.Count; i++)
            {
                puzzleGridControlList[i].update();
            }
        }

        /// <summary>
        /// passing in the difficulty to create the puzzle 
        /// </summary>
        private string difficulty;
        public string Difficulty
        {
            get
            {
                return difficulty;
            }
            set
            {
                //sets the difficulty and makes the puzzle, maybe this should be handled differently
                difficulty = value;
                if (value != "Load")
                {
                    ZombieMonkeys.Vibria.Logic.PuzzleAssembler puzzleAssembler = new Logic.PuzzleAssembler();
                    Logic.Puzzle puzzle = puzzleAssembler.Assemble(9, 9, 9, difficulty);
                    PopulateGrid(puzzle);
                }
            }
        }

        /// <summary>
        /// Method to set values for variables in each square
        /// </summary>
        /// <param name="puzzle"></param>
        public void PopulateGrid(Logic.Puzzle puzzle)
        {
            foreach (Logic.Square s in puzzle.Grid.SquareList)
            {
                int i = s.GetRow();
                int j = s.GetColumn();
                int g = s.GetGrid();
                

                SquareControl squareControl = new SquareControl(i, j, g, s);
                squareControl.GetPuzzleGrid = this;
                squareControl.TextValue = s.GetValue();
                //makes each non zero value initial value permanent so it cannot be changed by the user
                if (s.GetValue() != "0")
                {
                    squareControl.Permanent = true;
                }
                squareControl.SquareClicked += new EventHandler(ChildBoxClicked);
                LayoutRoot.Children.Add(squareControl);
                Grid.SetRow(squareControl, i);
                Grid.SetColumn(squareControl, j);
                puzzleGridControlList.Add(squareControl);
                puzzleGridList.Add(s);
            }
        }
    }
}
